﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AForge.Video.DirectShow;
using AForge.Video;

namespace video_ai_2
{
    public partial class Form1 : Form
    {
        // opened video source
        private IVideoSource videoSource = null;
        // current statistics index
        private int statIndex = 0;
        // ready statistics values
        private int statReady = 0;

        FilterInfoCollection videoDevices;
        private string device;

        // Video device
        public string VideoDevice
        {
            get { return device; }
        }

        public Form1()
        {
            InitializeComponent();

            // show device list
            try
            {
                // enumerate video devices
                videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

                if (videoDevices.Count == 0)
                    throw new ApplicationException();

                // add all devices to combo
                foreach (FilterInfo device in videoDevices)
                {
                    comboBoxDevice.Items.Add(device.Name);
                }
            }
            catch (ApplicationException)
            {
                comboBoxDevice.Items.Add("Нет записывающих устройств.");
                comboBoxDevice.Enabled = false;
                buttonCapture.Enabled = false;
            }
            comboBoxDevice.SelectedIndex = 0;         
        }

        // Open video source
        private void OpenVideoSource(IVideoSource source)
        {
            // set busy cursor
            this.Cursor = Cursors.WaitCursor;

            // close previous video source
            //CloseVideoSource();
            
            // start new video source
            videoSourcePlayer.VideoSource = source;
            videoSourcePlayer.Start();

            // reset statistics
            statIndex = statReady = 0;

            videoSource = source;
            
            this.Cursor = Cursors.Default;
        }

        private void buttonCapture_Click(object sender, EventArgs e)
        {
            device = videoDevices[comboBoxDevice.SelectedIndex].MonikerString;

            // create video source
            VideoCaptureDevice videoSource = new VideoCaptureDevice(VideoDevice);

            // open it
            OpenVideoSource(videoSource);
        }

    }
}
